<script type="text/javascript">
	$(document).ready(function(){
		$("#mensaje").hide();
		$("#imgCarga").hide();
		$("#baceptar").show();
    $("#errorNick").hide();
    $("#errorPassword").hide();
    $("#errorCPassword").hide();
    $("#errorEmail").hide();
    $("#errorNombre").hide();
    $("#errorCiudad").hide();
		$("#baceptar").click(registrarUsuario);
	});

  function validaUsuario()
  {
    var valida = true;
    var patronLetras =/^[a-zA-Z]+$/;
    var patronNombre = /^[a-zA-Z\ \'\u00e1\u00e9\u00ed\u00f3\u00fa\u00c1\u00c9\u00cd\u00d3\u00da\u00f1\u00d1\u00FC\u00DC]*$/;
    var patronEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/ ;
    if($("#nick").val().length == 0 || !patronLetras.test($("#nick").val()))
    {
      valida = false;
      $("#errorNick").html("Solo se admiten letras");
      $("#errorNick").show();
    }else{
      $("#errorNick").hide();
    }
    if($("#password").val().length < 6)
    {
      valida = false;
      $("#errorPassword").html("El password debe ser de 6 caracteres como minimo");
      $("#errorPassword").show();
    }else{
      $("#errorPassword").hide();
    }
    if($("#password").val() != $("#cpassword").val())
    {
      valida = false;
      $("#errorCPassword").html("No se ha introducido el mismo password");
      $("#errorCPassword").show();
    }else{
      $("#errorCPassword").hide();
    }
    if($("#email").val().length == 0 || !patronEmail.test($("#email").val()))
    {
      valida = false;
      $("#errorEmail").html("Introduzca un email correcto");
      $("#errorEmail").show();
    }else{
      $("#errorEmail").hide();
    }

    if($("#nombre").val().length == 0 ||!patronNombre.test($("#nombre").val()))
    {
      valida = false;
      $("#errorNombre").html("Introduzca un nombre correcto");
      $("#errorNombre").show();
    }else{
      $("#errorNombre").hide();
    }

    if($("#ciudad").val().length == 0 || !patronNombre.test($("#ciudad").val()))
    {
      valida = false;
      $("#errorCiudad").html("Introduzca una ciudad correcta");
      $("#errorCiudad").show();
    }else{
      $("#errorCiudad").hide();
    }

    return valida;
  }


	function registrarUsuario()
	{
    if(validaUsuario())
    {
      $("#errorNick").hide();
      $("#errorPassword").hide();
      $("#errorCPassword").hide();
      $("#errorEmail").hide();
      $("#errorNombre").hide();
      $("#errorCiudad").hide();

  		$.ajax({
    			type: "POST",
    			url: "modulos/usuarios/regusuaction.php",
    			data: {
    				nick: $("#nick").val(),
    				password: $("#password").val(),
    				email: $("#email").val(),
    				nombre: $("#nombre").val(),
    				sexo: $("#sexo").val(),
    				nacimiento: $("#nacimiento").val(),
    				ciudad: $("#ciudad").val(),
    				pais: $("#pais").val()
    			},
    			beforeSend: function(){
    				$("#mensaje").hide();
    				$("#imgCarga").show();
    				$("#baceptar").hide();
    			},
    			success: function(resp){
    				$("#imgCarga").hide();
    				$("#baceptar").hide();
    				if (resp == "ok")
    				{
    					$("#mensaje").html("El usuario se ha registrado correctamente");
    				}else{
    					$("#mensaje").html(resp);
    				}
    				$("#baceptar").show();
    				$("#mensaje").show();
    			}
  		});
    }
	}
</script>
<div id="titulos">
    <div id="fontit">
      <h3>Registro de Usuario</h3>
    </div>
  </div>
<div id="contenidoRegis" class="registr">
  <form  class="pure-form pure-form-stacked">
	 <div><label for="nick">Nick: </label><input id="nick" name="nick" type="text" maxlength="15"></div>
   <div id="errorNick"></div>
	 <div><label for="password">Password: </label><input id="password" name="password" type="password" maxlength="15"></div>
   <div id="errorPassword"></div>
   <div><label for="cpassword">Confirmar Password: </label><input id="cpassword" name="cpassword" type="password" maxlength="15"></div>
   <div id="errorCPassword"></div>
	 <div><label for="email">Email: </label><input id="email" name="email" type="text" maxlength="50"></div>
   <div id="errorEmail"></div>
	 <div><label for="nombre">Nombre: </label><input id="nombre" name="nombre" type="text" maxlength="50"></div>
   <div id="errorNombre"></div>
	 <div>
		<label for="nick">Sexo: </label>
		<select id="sexo" name="sexo">
			<option value="hombre" selected>Hombre</option>
			<option value="mujer">Mujer</option>
		</select>
	</div>
	<div><label for="nacimiento">A&ntilde;o nacimiento: </label>
      <select name="nacimiento" id="nacimiento">
      <?php
      for($anyo=(date("Y")); 1920<=$anyo; $anyo--) {
        echo "<option value='".$anyo."'>".$anyo."</option>";
      }
      ?>
    </select>
  </div>
	<div><label for="ciudad">Ciudad: </label><input id="ciudad" name="ciudad" type="text" maxlength="50"></div>
  <div id="errorCiudad"></div>
	<div><label for="pais">Pais: </label>
    <select name="pais" id="pais">
    <?php
      $array_paises = array("Estados Unidos","Espa&ntilde;a","Republica Dominicana","Afganistan","Africa del Sur","Albania","Alemania","Andorra","Angola","Antigua y Barbuda","Antillas Holandesas","Arabia Saudita","Argelia","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarusia","Belgica","Belice","Benin","Bermudas","Bolivia","Bosnia","Botswana","Brasil","Brunei Darussulam","Bulgaria","Burkina Faso","Burundi","Butan","Camboya","Camerun","Canada","Cape Verde","Chad","Chile","China","Chipre","Colombia","Comoros","Congo","Corea del Norte","Corea del Sur","Costa de Marf&iacute;l","Costa Rica","Croasia","Cuba","Dinamarca","Djibouti","Dominica","Ecuador","Egipto","El Salvador","Emiratos Arabes Unidos","Eritrea","Eslovenia","Estonia","Etiopia","Fiji","Filipinas","Finlandia","Francia","Gabon","Gambia","Georgia","Ghana","Granada","Grecia","Groenlandia","Guadalupe","Guam","Guatemala","Guayana Francesa","Guerney","Guinea","Guinea-Bissau","Guinea Equatorial","Guyana","Haiti","Holanda","Honduras","Hong Kong","Hungria","India","Indonesia","Irak","Iran","Irlanda","Islandia","Islas Caiman","Islas Faroe","Islas Malvinas","Islas Marshall","Islas Solomon","Islas Virgenes Britanicas","Islas Virgenes (U.S.)","Israel","Italia","Jamaica","Japon","Jersey","Jordania","Kazakhstan","Kenia","Kiribati","Kuwait","Kyrgyzstan","Laos","Latvia","Lesotho","Libano","Liberia","Libia","Liechtenstein","Lituania","Luxemburgo","Macao","Macedonia","Madagascar","Malasia","Malawi","Maldivas","Mali","Malta","Marruecos","Martinica","Mauricio","Mauritania","Mexico","Micronesia","Moldova","Monaco","Mongolia","Mozambique","Myanmar (Burma)","Namibia","Nepal","Nicaragua","Niger","Nigeria","Noruega","Nueva Caledonia","Nueva Zealandia","Oman","Pakistan","Palestina","Panama","Papua Nueva Guinea","Paraguay","Peru","Polinesia Francesa","Polonia","Portugal","Puerto Rico","Qatar","Reino Unido","Republica Centroafricana","Republica Checa","Republica Democratica del Congo","Republica Eslovaca","Reunion","Ruanda","Rumania","Rusia","Sahara","Samoa","San Cristobal-Nevis (St. Kitts)","San Marino","San Vincente y las Granadinas","Santa Helena","Santa Lucia","Santa Sede (Vaticano)","Sao Tome & Principe","Senegal","Seychelles","Sierra Leona","Singapur","Siria","Somalia","Sri Lanka (Ceilan)","Sudan","Suecia","Suiza","Sur Africa","Surinam","Swaziland","Tailandia","Taiwan","Tajikistan","Tanzania","Timor Oriental","Togo","Tokelau","Tonga","Trinidad & Tobago","Tunisia","Turkmenistan","Turquia","Ucrania","Uganda","Union Europea","Uruguay","Uzbekistan","Vanuatu","Venezuela","Vietnam","Yemen","Yugoslavia","Zambia","Zimbabwe");
      $cantidad_paises = count($array_paises);
      for($i = 0; $i<$cantidad_paises; $i++){
          $array_paises_i = $array_paises[$i];
          echo "<option value='".$array_paises_i."'>".$array_paises_i."</option>";
      }
      ?>
    </select>
  </div>
	<input id="baceptar" type="button" value="Aceptar" class="pure-button notice">
	<img id="imgCarga" src="estilos/img/carga.gif">
	<div id="mensaje"></div>
  </form>
</div>